package com.meitianhui.convenitenbuy.module.wallet.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.base.BaseListResponse;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.http.MainApiDataSource;
import com.meitianhui.convenitenbuy.module.mine.model.UserAsset;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletGoldContract;
import com.meitianhui.convenitenbuy.module.wallet.http.WalletApiDataSource;
import com.meitianhui.convenitenbuy.module.wallet.model.PropertyBean;
import com.meitianhui.convenitenbuy.module.wallet.ui.activity.WalletGoldActivity;
import com.meitianhui.convenitenbuy.util.PreferenceUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2017/11/2.
 */

public class WalletGoldPresenter implements WalletGoldContract.Presenter {

    private WalletGoldActivity context;
    private final WalletGoldContract.View view;
    private Gson gson;

    public WalletGoldPresenter(WalletGoldActivity view, WalletGoldActivity context) {
        this.view = view;
        this.context = context;
        gson = new Gson();
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }


    @Override
    public void getGoldRecord(int pageNum) {
        String app_token = InfoProvider.getAppToken(App.getInstance());
        String service = "finance.memberGoldLogPageFind";

        Map<String, String> pageString = new HashMap<>();
        pageString.put("page_no", pageNum + "");
        pageString.put("page_size", "20");
        String page = gson.toJson(pageString);

        Map<String, String> paramsString = new HashMap<>();
        paramsString.put("member_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        paramsString.put("member_type_key", "consumer");
        String params = gson.toJson(paramsString);

        WalletApiDataSource.getInstance()
                .getGoldDetail(app_token, service, page, params)
                .subscribe(new ResponseObserver<BaseListResponse<PropertyBean>>(context, true) {
                    @Override
                    public void onSuccess(BaseListResponse<PropertyBean> response) {
                        List<PropertyBean> list = response.data.getList();
                        view.getGoldRecordBack(list);
                    }
                });
    }

    @Override
    public void getGold() {
        view.getGoldBack(InfoProvider.getUserAsset(App.getInstance()));
        Map<String, String> map = new HashMap<>();
        map.put("member_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        String json = new Gson().toJson(map);

        MainApiDataSource.getInstance()
                .getUserAsset(InfoProvider.getUserToken(App.getInstance()).getUser_token(),
                        "finance.memberAssetFind", json)
                .subscribe(new ResponseObserver<BaseResponse<UserAsset>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<UserAsset> response) {
                        view.getGoldBack(response.data);
                        PreferenceUtils.saveObject(
                                App.getInstance(), Constants.USER_ASSET, response.data);
                    }
                });
    }

}
